package com.njmd.framework.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 
 * @title:时间日期工具类
 * @description:
 * 
 * @author: Yao
 * 
 */
public final class DateTimeUtil
{
	/**
	 * 获得8位的日期 yyyyMMdd
	 * 
	 * @return
	 */
	public static String getChar8()
	{
		return DateFormatUtils.format(new Date(), "yyyyMMdd");
	}

	/**
	 * 获得14位的日期 yyyyMMddHHmmss
	 * 
	 * @return
	 */
	public static String getChar17()
	{
		return DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS");
	}

	/**
	 * 获得14位的日期 yyyyMMddHHmmss
	 * 
	 * @return
	 */
	public static String getChar14()
	{
		return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
	}

	/**
	 * 获得12位的日期 yyyyMMddHHmm
	 * 
	 * @return
	 */
	public static String getChar12()
	{
		return DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
	}

	/**
	 * 格式化14位的日期
	 * 
	 * @param char14
	 * @return
	 */
	public static String formatChar14(String char14)
	{
		if (char14 == null || char14.length() == 0)
			return char14;
		return char14.substring(0, 4) + "-" + char14.substring(4, 6) + "-" + char14.substring(6, 8) + " "
				+ char14.substring(8, 10) + ":" + char14.substring(10, 12) + ":" + char14.substring(12, 14) + " ";
	}

	/**
	 * 格式化8位的日期
	 * 
	 * @param char8
	 * @return
	 */
	public static String formatChar8(String char8)
	{
		if (char8 == null || char8.length() == 0)
			return char8;
		return char8.substring(0, 4) + "-" + char8.substring(4, 6) + "-" + char8.substring(6, 8) + " ";

	}
	
	
	public static String formatDate(Date date, String format)
	{
		return DateFormatUtils.format(date, format, Locale.ENGLISH);
	}

	public static String formatDataBaseChar8(String char8)
	{
		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd ");
		String time = null;
		try
		{
			time = DateFormatUtils.format(formate.parse(char8), "yyyyMMdd");
		}
		catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time;
	}

	public static String formatDataBaseChar12(String char8)
	{
		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String time = null;
		try
		{
			time = DateFormatUtils.format(formate.parse(char8), "yyyyMMddHHmm");
		}
		catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 返回当天所在的年份
	 * 
	 * @return yyyy
	 */
	public static String getCurrentYear()
	{
		return getChar8().substring(0, 4);
	}

	/**
	 * 返回当天所在的年月
	 * 
	 * @return yyyyMM
	 */
	public static String getCurrentYearMonth()
	{
		return getChar8().substring(0, 6);
	}

	/**
	 * 返回当天所在的月份
	 * 
	 * @return mm
	 */
	public static String getCurrentMonth()
	{
		return getChar8().substring(4, 6);
	}

	/**
	 * 返回指定时间所在月份
	 * 
	 * @param date
	 * @return
	 */
	public static String getAssignMonth(String date)
	{
		if (date != null && date.length() >= 6)
			return date.substring(4, 6);

		return null;
	}

	/**
	 * 根据指定规则格式化日期
	 * 
	 * @param date
	 * @param formatter
	 * @return
	 */
	public static String formatDate(String date, String formatter)
	{
		SimpleDateFormat myFormatter = null;
		Date da = null;
		if (date.length() < 15)
			myFormatter = new SimpleDateFormat("yyyyMMddHHmmss");
		else
			myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
			da = myFormatter.parse(date);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return DateFormatUtils.format(da, formatter);
	}

	public static Date parse(String dateStr, String formatter)
	{
		SimpleDateFormat format = new SimpleDateFormat(formatter);
		try
		{
			return format.parse(dateStr);
		}
		catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 将当前日期向后滚动n天
	 * @param dateNow
	 * @param rollDate
	 * @return
	 */
	public static String rollDate(String dateNow, int rollDate) {
		String dateReturn = "";
		if (dateNow == null || dateNow.trim().length() < 8)
			return dateReturn;

		dateNow = dateNow.trim();
		Calendar cal = Calendar.getInstance();
		int nYear = Integer.parseInt(dateNow.substring(0, 4));
		int nMonth = Integer.parseInt(dateNow.substring(4, 6));
		int nDate = Integer.parseInt(dateNow.substring(6, 8));
		cal.set(nYear, nMonth - 1, nDate);
		cal.add(Calendar.DATE, rollDate);
		String strYear = String.valueOf(cal.get(Calendar.YEAR));
		String strMonth = String.valueOf(cal.get(Calendar.MONTH) + 1);
		String strDay = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		strMonth = (strMonth.length() == 1) ? "0" + strMonth : strMonth;
		strDay = (strDay.length() == 1) ? "0" + strDay : strDay;
		dateReturn = strYear + strMonth + strDay;
		return dateReturn;
	}
	/**
	 * 获取月份的最后一天
	 * @param year
	 * @param month
	 * @return
	 */
	public static int lastDay(Integer year, int month) {
		  
		Calendar cal = Calendar.getInstance(); 
		cal.set(year, month - 1, 1); 
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DATE, -1); 
		return cal.get(Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * fetch date from text
	 * 
	 * @param text
	 * @return
	 */
	public static Date fetchDateFromText(String text)
	{
		if (text == null || "".equals(text))
			return null;
		else
			text = text.trim();

		Date resultDate = null;

		final String[] DATE_FORMAT = { "MMM d,yyyy", "MMM. d,yyyy", "MMM dd,yyyy", "MMM yyyy", "M/d/yy", "M/d/yyyy", "MM/dd/yyyyy",
				"yyyy - M","yyyyMMddHHmmss" };
		List<String> regList = new ArrayList<String>();
		regList.add("(^|.+)(\\d{1,2}/\\d{1,2}/\\d{2,4})($|.+)");
		regList.add("(^|.+)((January|Jan|February|Feb|March|Mar|April|Apr|May|June|Jun|July|Jul|August|Aug|September|Sept|Sep|October|Oct|November|Nov|December|Dec).+20\\d{2})($|.+)");
		regList.add("(^|.+)(\\d{4} - \\d{1,2})($|.+)");
		
		regList.add("(^|.+)(((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})([-\\/\\._]{0,1})(((0[13578]|1[02])([-\\/\\._]{0,1})(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)([-\\/\\._]{0,1})(0[1-9]|[12][0-9]|30))|(02([-\\/\\._]{0,1})(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))([-\\/\\._]{0,1})02([-\\/\\._]{0,1})29))\\s{0,1}(([01][0-9]|2[0-3])\\:{0,1}([0-5][0-9])\\:{0,1}([0-5][0-9])))($|.+)");
		
		for (String reg : regList)
		{
			try
			{
				Pattern datePattern = Pattern.compile(reg.toString(), Pattern.CASE_INSENSITIVE);
				Matcher dateMatcher = datePattern.matcher(text);
				if (dateMatcher.find())
				{
					String textDate = dateMatcher.group(2);
					resultDate = parseToDate(textDate, DATE_FORMAT);
					break;
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}

		return resultDate;
	}

	public static String fetchDateFromText(String text, String formatPattern)
	{
		Date date = fetchDateFromText(text);
		if (date != null)
		{
			return DateTimeUtil.formatDate(date, formatPattern);
		}
		else
			return null;

	}
	
	/**
	 * parse to date from date string
	 * 
	 * @param dateStr
	 * @param pattern
	 * @return
	 */
	public static Date parseToDate(String dateStr, String pattern)
	{
		String[] patterns = { pattern };
		return parseToDate(dateStr, patterns);
	}

	/**
	 * parse to date from date string
	 * 
	 * @param dateStr
	 * @param patterns
	 * @return
	 */
	public static Date parseToDate(String dateStr, String[] patterns)
	{
		Date result = null;
		DateFormat format = null;
		for (String pattern : patterns)
		{
			try
			{
				format = new SimpleDateFormat(pattern, Locale.ENGLISH);
				result = format.parse(dateStr);
				return result;
			}
			catch (ParseException e)
			{
				continue;
			}
		}
		return result;
	}
	
	public static String convertToNewFormat(String dateStr, String[] parsePattens, String formatPatten)
	{
		Date date = parseToDate(dateStr, parsePattens);
		if (date != null)
		{
			DateFormat format = new SimpleDateFormat(formatPatten, Locale.ENGLISH);
			return format.format(date);
		}
		else
			return null;
	}
	
	public static void main(String args[]){
		String yyyyMMddHHmmss="((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})([-\\/\\._]{0,1})(((0[13578]|1[02])([-\\/\\._]{0,1})(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)([-\\/\\._]{0,1})(0[1-9]|[12][0-9]|30))|(02([-\\/\\._]{0,1})(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))([-\\/\\._]{0,1})02([-\\/\\._]{0,1})29))\\s{0,1}(([01][0-9]|2[0-3])\\:{0,1}([0-5][0-9])\\:{0,1}([0-5][0-9]))"; 
		

		Pattern datePattern = Pattern.compile(yyyyMMddHHmmss);
		Matcher dateMatcher = datePattern.matcher("20130424160205");
		if (dateMatcher.find())
		{
			String textDate = dateMatcher.group();
			System.out.println(textDate);
		}
		
		System.out.println(fetchDateFromText("A205689_000000201304241202061","yyyyMMddHHmmss"));
		
	}
}
